﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Caifu.Fx;
using Caifu.Clients;
using System.Web.SessionState;
using Itfort;
namespace Caifu.WebSite
{
    /// <summary>
    /// Login 的摘要说明
    /// </summary>
    public class Login : BaseHandler, IRequiresSessionState
    {
        public override void ProcessRequest(HttpContext context)
        {
            RequestResult result = null;
            base.ProcessRequest(context);
            PostCount++;
            if (Request.RequestType == "POST" || Request.RequestType == "GET")
            {
                result = CheckRequest(Request["name"], Request["pwd"]);
                if (result.Status == 20000)
                {
                    result = Authorize(Request["name"], Request["pwd"]);
                }
            }
            else
            {
                result = new RequestResult(40500);
            }
            SendJosn(result);
        }

        /// <summary>
        /// 检查提交的表单
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>响应的结果体</returns>
        private RequestResult CheckRequest(String name, String password)
        {
            if (PostCount > MAXREQUEST + 1)
            {
                if (String.IsNullOrEmpty(Request["verifyCode"]) ||
                    !(new ValidateCodeManager(HttpContext.Current))
                    .ValidationCode(Request["verifyCode"], "log"))
                {
                    return new RequestResult(40003);
                }
            }
            if (String.IsNullOrEmpty(name))
            {
                return new RequestResult(40001);
            }
            if (String.IsNullOrEmpty(password))
            {
                return new RequestResult(40002);
            }
            return new RequestResult();
        }

        /// <summary>
        /// 登录，授权
        /// </summary>
        private RequestResult Authorize(String mail, String password)
        {
            var user = UserClient.Instance.Login(mail, MD5Hash.ComputeHash(password));
            if (user != null)
            {
                var ticket=Comb.NewComb();
                if (SsoClient.Instance.GetLoginInfoByUserId(user.Id) != null)
                {
                    SsoClient.Instance.UpdateSsoInfo(user.Id, ticket);
                }
                else
                {
                    SsoClient.Instance.AddSsoInfo(user.Id, ticket);
                }
                return new RequestResult(20000, user.Email, ticket.ToString("N"));
            }
            return new RequestResult(40100);
        }

        /// <summary>
        /// SendJosn
        /// </summary>
        private void SendJosn(RequestResult result)
        {
            Response.ContentType = "text/plain";
            if (PostCount > MAXREQUEST)
                result.Captcha = true;
            String Josn = "var loginResult={0}s:{1},e:'{2}',t:'{3}',m:'{4}',Captcha:{5}{6};";
            Response.Write(String.Format(Josn, "{", result.Status, result.Email,
                result.Ticket, result.Msg, result.Captcha.ToString().ToLower(), "}"));
            Response.End();
        }
    }
}